package service

import (
	"github.com/gin-gonic/gin"
	"talentsystem/model"
	"github.com/astaxie/beego/orm"
)

func PerfectYourResume(c*gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	num,err:=orm.NewOrm().QueryTable("resume").Filter("account_id",param.Id).Count()
	if err!=nil {
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
	}
	var result model.PerfectYourResume
	if num ==0{
		resume:=new(model.Resume)
		resume.AccountId=param.Id
		_,err:=orm.NewOrm().Insert(resume)
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
		resumehope:=new(model.ResumeHope)
		resumehope.ResumeId=param.Id
		_,err=orm.NewOrm().Insert(resumehope)
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
		resumeexperence:=new(model.ResumeExperence)
		resumeexperence.ResumeId=param.Id
		_,err=orm.NewOrm().Insert(resumeexperence)
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
		resumeeducateion:=new(model.ResumeEducation)
		resumeeducateion.ResumeId=param.Id
		_,err=orm.NewOrm().Insert(resumeeducateion)
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
		resumeability:=new(model.ResumeAbility)
		resumeability.ResumeId=param.Id
		_,err=orm.NewOrm().Insert(resumeability)
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 200,
			"data":    result,
		})
		return
	}else if num ==1{
		err:=orm.NewOrm().Raw("select resume.*,resume_ability.*,resume_education.edueational as studyeducational,resume_education.majors,resume_education.school,resume_education.school_experience,resume_education.study_start_time,resume_education.study_end_time,resume_experence.*,resume_hope.* from resume_ability,resume,resume_education,resume_experence,resume_hope where resume.account_id=? and resume_ability.resume_id=resume.account_id and resume_education.resume_id=resume.account_id and resume_experence.resume_id=resume.account_id and resume_hope.resume_id=resume.account_id",param.Id).QueryRow(&result)
		if err!=nil {
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
	}
	c.JSON(200, gin.H{
		"status": 200,
		"data":  result,
	})
}
func UpdateYourResume(c*gin.Context){
	var param model.UpdateYourResume
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	/*if param.Id==0{
		resume:=new(model.Resume)
		resume.ResumeName=param.ResumeName
		resume.Educational=param.Educational
		resume.WorkYears=param.WorkYears
		resume.Company=param.Company
		resume.City=param.City
		resume.Industry=param.Industry
		resume.Position=param.Position
		resume.PositionState=param.PositionState
		resume.Salary=param.Salary
		resume.Phone=param.Phone
		resume.UserName=param.UserName
		resume.Age=param.Age
		resume.Sex=param.Sex
		resume.AccountId=param.AccountId
		_,err:=orm.NewOrm().Insert(resume)
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 200,
			"msg":1,
		})
		return
	}*/
	o:=orm.NewOrm()
	_,err:=o.QueryTable("resume").Filter("account_id",param.AccountId).Update(orm.Params{
		"ResumeName":param.ResumeName,
		"Educational":param.Educational,
		"WorkYears":param.WorkYears,
		"Company":param.Company,
		"City":param.City,
		"Industry":param.Industry,
		"Position":param.Position,
		"PositionState":param.PositionState,
		"Salary":param.Salary,
		"Phone":param.Phone,
		"UserName":param.UserName,
		"Age":param.Age,
		"Sex":param.Sex,
	})
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":1,
	})
	return
}
func UpdateYourResumeHope(c*gin.Context){
	var param model.UpdateYourResumeHope
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	/*if param.Id==0{
		resumehope:=new(model.ResumeHope)
		resumehope.HopeIndustry=param.HopeIndustry
		resumehope.HopePosition=param.HopePosition
		resumehope.HopeCity=param.HopeCity
		resumehope.HopeSalary=param.HopeSalary
		resumehope.ResumeId=param.AccountId
		_,err:=orm.NewOrm().Insert(resumehope)
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 200,
			"msg":1,
		})
		return
	}*/
	o:=orm.NewOrm()
	_,err:=o.QueryTable("resume_hope").Filter("resume_id",param.AccountId).Update(orm.Params{
		"HopeIndustry":param.HopeIndustry,
		"HopePosition":param.HopePosition,
		"HopeCity":param.HopeCity,
		"HopeSalary":param.HopeSalary,
	})
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":1,
	})
	return
}
func UpdateYourResumeExperence(c*gin.Context){
	var param model.UpdateYourResumeExperence
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	/*if param.Id==0{
		resumeexperence:=new(model.ResumeExperence)
		resumeexperence.CompanyName=param.CompanyName
		resumeexperence.CompanyIndustry=param.CompanyIndustry
		resumeexperence.PositionName=param.PositionName
		resumeexperence.WorkStartTime=param.WorkStartTime
		resumeexperence.WorkEndTime=param.WorkEndTime
		resumeexperence.Performance=param.Performance
		resumeexperence.ResumeId=param.AccountId
		_,err:=orm.NewOrm().Insert(resumeexperence)
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 200,
			"msg":1,
		})
		return
	}*/
	o:=orm.NewOrm()
	_,err:=o.QueryTable("resume_experence").Filter("resume_id",param.AccountId).Update(orm.Params{
		"CompanyName":param.CompanyName,
		"CompanyIndustry":param.CompanyIndustry,
		"PositionName":param.PositionName,
		"WorkStartTime":param.WorkStartTime,
		"WorkEndTime":param.WorkEndTime,
		"Performance":param.Performance,
	})
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":1,
	})
	return
}
func UpdateYourResumeEducation(c*gin.Context){
	var param model.UpdateYourResumeEducation
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	/*if param.Id==0{
		resumeeducateion:=new(model.ResumeEducation)
		resumeeducateion.School=param.School
		resumeeducateion.Majors=param.Majors
		resumeeducateion.Edueational=param.Edueational
		resumeeducateion.StudyStartTime=param.StudyStartTime
		resumeeducateion.StudyEndTime=param.StudyEndTime
		resumeeducateion.SchoolExperience=param.SchoolExperience
		resumeeducateion.ResumeId=param.AccountId
		_,err:=orm.NewOrm().Insert(resumeeducateion)
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 200,
			"msg":1,
		})
		return
	}*/
	o:=orm.NewOrm()
	_,err:=o.QueryTable("resume_education").Filter("resume_id",param.AccountId).Update(orm.Params{
		"School":param.School,
		"Majors":param.Majors,
		"Edueational":param.Edueational,
		"StudyStartTime":param.StudyStartTime,
		"StudyEndTime":param.StudyEndTime,
		"SchoolExperience":param.SchoolExperience,
	})
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":1,
	})
	return
}
func UpdateYourResumeAbility(c*gin.Context){
	var param model.UpdateYourResumeAbility
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	/*if param.Id==0{
		resumeability:=new(model.ResumeAbility)
		resumeability.AbilityName=param.AbilityName
		resumeability.AbiltyContent=param.AbiltyContent
		resumeability.ResumeId=param.AccountId
		_,err:=orm.NewOrm().Insert(resumeability)
		if err!= nil{
			c.JSON(500, gin.H{
				"status": 500,
				"msg":    "系统错误,请刷新重试",
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 200,
			"msg":1,
		})
		return
	}*/
	o:=orm.NewOrm()
	_,err:=o.QueryTable("resume_ability").Filter("resume_id",param.AccountId).Update(orm.Params{
		"AbilityName":param.AbilityName,
		"AbiltyContent":param.AbiltyContent,
	})
	if err!= nil{
		c.JSON(500, gin.H{
			"status": 500,
			"msg":    "系统错误,请刷新重试",
		})
		return
	}
	c.JSON(200, gin.H{
		"status": 200,
		"msg":1,
	})
	return
}